/*
 * @Author: hexudong 1264275442@qq.com
 * @Date: 2023-04-11 17:58:25
 * @LastEditors: hexudong 1264275442@qq.com
 * @LastEditTime: 2023-04-20 15:39:13
 * @FilePath: /worker_supervision_oa/src/components/BorrowDetail/processModal.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Form, Radio, Input, Button, Select, Avatar, message, InputNumber } from 'antd';
import FileUpload from '@/components/FileUpload';
import { handleFileid, accDiv, accMul } from '@/utils/utils';
import React, { useState, useEffect } from 'react';
import { borrowCheck, companysLists } from '@/services/public'
import NumberInput from '@/components/NumberInput';

const FormItem = Form.Item;
const formItemLayout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 14 },
};

interface ProcessModalProps {
    type: string,
    detailMessage: any
    onSubmit: () => void
}
const ProcessModal: React.FC<ProcessModalProps> = ({ type, detailMessage, onSubmit }) => {
    const [form] = Form.useForm();
    const [companys, setCompanys] = useState<any>([])
    const [amount, setAmount] = useState<any>();
    const [radioValue, setRadioValue] = useState(1);
    const tailLayout = {
        wrapperCol: { offset: 8, span: 8 },
    };
    const reconfirm = async () => {
        try {
            const fieldsValue: any = await form.validateFields();
            let params:any = {}
            console.log(fieldsValue, 'fieldsValue');
            switch (type) {
                case 'bank':
                    params = {
                        id: detailMessage.id,
                        check: fieldsValue.check,
                        remark: fieldsValue.remark,
                        checkFileId: fieldsValue.dataFile ? handleFileid(fieldsValue.dataFile) : '',
                    }
                    if (radioValue!=0) {
                        params.bankCheckAmount = fieldsValue.bankCheckAmount * 100
                        params.bankCheckTimeLimit = fieldsValue.bankCheckTimeLimit
                    }
                    break;
                case 'labor':
                    params = {
                        id: detailMessage.id,
                        remark: fieldsValue.remark,
                        checkFileId: fieldsValue.dataFile ? handleFileid(fieldsValue.dataFile) : ''
                    }
                    break;
                case 'guarantee':
                    params = {
                        id: detailMessage.id,
                        check: fieldsValue.check,
                        remark: fieldsValue.remark,
                        checkFileId: fieldsValue.dataFile ? handleFileid(fieldsValue.dataFile) : ''

                    }
                    break;
                case 'platform':
                    params = {
                        id: detailMessage.id,
                        check: fieldsValue.check,
                        remark: fieldsValue.remark,
                        checkFileId: fieldsValue.dataFile ? handleFileid(fieldsValue.dataFile) : '',
                        guaranteeCorpId: fieldsValue.guaranteeCorpId
                    }
                    break;
                default:

                    break;
            }

            let res = await borrowCheck(params, type)
            if (res && res.success) {
                // history.goBack()
                onSubmit();
            } else {
                message.error(res.errMsg || '');
            }


        } catch (error) {

        }
    }
    const getLists = async () => {
        let res = await companysLists({ type: 1 })
        if (res && res.success) {
            let list: any[] = []
            list = res.data.map((el: any) => {
                return {
                    label: el.name,
                    value: el.id,
                    avatar: el.url
                }
            })
            setCompanys(list)
            console.log(list, 'list');

        }


    }
    const changeRadio = (e: any) => {
        console.log(e, 'ee');
        setRadioValue(e.target.value);

    }
    useEffect(() => {
        getLists()
        if (detailMessage.guaranteeAmount) {
            const am = accMul(accDiv(detailMessage.guaranteeAmount, 100), 0.24);
            setAmount(am);
        }
    }, [])
    return (
        <>
            <Form {...formItemLayout} name="save_project" form={form}>
                {
                    type !== 'labor' && (
                        <FormItem
                            label="审核意见"
                            name="check"
                            initialValue={1}
                            rules={[{ required: true, message: '请选择审核意见！' }]}
                        >
                            <Radio.Group onChange={changeRadio}>
                                <Radio value={1}>通过</Radio>
                                <Radio value={0}>拒绝</Radio>
                            </Radio.Group>
                        </FormItem>
                    )
                }
                {
                    type == 'platform' && (
                        <FormItem label="担保公司" name="guaranteeCorpId" >
                            <Select placeholder="请选择项目所属担保公司">
                                {companys.map((item: any) => (
                                    <Select.Option key={item?.value} value={item?.value}>
                                        <Avatar src={item.avatar} />
                                        <span style={{ marginLeft: '10px' }}>{item.label}</span>
                                    </Select.Option>
                                ))}


                            </Select>
                        </FormItem>
                    )
                }
                {
                    type == 'bank' && radioValue != 0 && (
                        <FormItem
                            label="贷款金额(元)"
                            name="bankCheckAmount"
                            initialValue={''}
                            help={`最高可贷金额=中标价或发包价 X 30% X 80%,${amount ? `当前最高${amount}元` : ''}`}
                            rules={[
                                { required: true, message: '请输入贷款金额(元)！' },
                                amount
                                    ? {
                                        type: 'number',
                                        max: amount,
                                        message: '不能超过最高可贷金额',
                                        transform: (value) => {
                                            return Number(value);
                                        }
                                    }
                                    : {},
                            ]}
                        >
                            <NumberInput style={{ width: '100%' }} placeholder="请输入贷款金额(单位 元)" />
                        </FormItem>
                    )
                }
                {
                    type == 'bank' && radioValue != 0 && (
                        <FormItem
                            label="贷款期限（月）"
                            initialValue={''}
                            name="bankCheckTimeLimit"
                            rules={[{ required: true, message: '请输入贷款期限！' }, {
                                pattern: /^[0-9]*[1-9][0-9]*$/,
                                message: '请输入整数月份！',
                            },]}
                        >
                            <InputNumber
                                style={{ width: '100%' }}
                                min="0"
                                placeholder="贷款期限"
                                addonAfter="个月"
                            />
                        </FormItem>
                    )
                }

                <FormItem
                    label="资料文件"
                    name="dataFile"
                    initialValue={[]}
                    rules={[{ required: type == 'labor', message: '请输入审核备注！' }]}

                >
                    <FileUpload ossEnum="PROJECT" multiple={true} max={6} type="image/*,.pdf,.doc,.docx" />
                </FormItem>


                <FormItem label="审核备注" name="remark" initialValue="" rules={[{ required: true, message: '请输入审核备注！' }]}>
                    <Input.TextArea showCount maxLength={100} />
                </FormItem>
                <FormItem {...tailLayout} style={{ textAlign: 'center' }}>
                    <Button type="primary" onClick={() => reconfirm()}>确定</Button>
                </FormItem>
            </Form>
        </>
    );
};

export default ProcessModal;